package com.dmp.pushlogs.bolt;

import com.dmp.common.date.DateUtils;
import com.dmp.storm.bolt.BaseMapBolt;

import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName customBolt
 * @Description
 *
 * @author sunzy
 * @date 2016年4月14日 下午7:49:23
 * @version 1.0
 *
 */
public class IsSuccessCustomBolt extends BaseMapBolt {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1797212540641047767L;

	private String custFlag = "1";
	
	@Override
	public Map<String, Object> process(Map<String, Object> map) {
		Map<String, Object> record = map;
		try {
			// start_time 毫秒-> yyyyMMddHHmmss
			String timeStr = (String) map.get("fe_time");
			long timeL = Long.valueOf(timeStr).longValue();
			Date date = new Date(timeL);
			timeStr = DateUtils.formatDate(date, "yyyyMMddHHmmss");
			record.put("fe_time", timeStr);

			// end_time 毫秒-> yyyyMMddHHmmss
			timeStr = (String) map.get("push_time");
			timeL = Long.valueOf(timeStr).longValue();
			Date date1 = new Date(timeL);
			timeStr = DateUtils.formatDate(date1, "yyyyMMddHHmmss");
			record.put("push_time", timeStr);

			// 其它时间
			record.put("hour", DateUtils.formatDate(date, "yyyyMMddHH"));
			record.put("hour_h", DateUtils.formatDate(date, "HH"));
			record.put("day", DateUtils.formatDate(date, "yyyyMMdd"));
			record.put("day_d", DateUtils.formatDate(date, "dd"));
			record.put("month", DateUtils.formatDate(date, "yyyyMM"));
			record.put("month_m", DateUtils.formatDate(date, "MM"));
			
			//日志时间
			record.put("log_date", DateUtils.formatDate(date1, "yyyy/MM/dd HH:mm:ss"));
			
			//去除需求id为空的
			String demandId = (String) map.get("ad_demand_id");
			if(!isNumeric(demandId)){
				record.put("ad_demand_id", "0");
			}
			System.out.println("IsSuccessCustomBolt1");
			// 客户标识
//			if("1".equals(this.custFlag)){
//				String custId = getCustIdByDemandId((String)record.get("demand_id"));
//				record.put("cust_id", custId);
//			}
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println(map);
			return null;
		}
		return record;
	}

	public String getCustFlag() {
		return custFlag;
	}

	public void setCustFlag(String custFlag) {
		this.custFlag = custFlag;
	}
	 public static boolean isNumeric(String str){
  	   if(str==null||str.equals("")){
  		   return false;  
  	   }
  	   Pattern pattern = Pattern.compile("[0-9]*"); 
  	   Matcher isNum = pattern.matcher(str);
  	   if( !isNum.matches() ){
  	       return false; 
  	   } 
  	   return true; 
  	}

}
